<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <j-popup
                v-model="model.name"
                code="query_product"
                field="name"
                orgFields="id,name,bar_code,number,unit_price,specifications,unit"
                destFields="productId,name,barCode,number,unitPrice,specifications,unit"
                :triggerChange="true"
                @callback="onPopCellChange(model.key, 'productList',$event)"
              >

              </j-popup>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input disabled v-model="model.number" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specifications">
              <a-input v-model="model.specifications" placeholder="请输入规格" />

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="条码" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="barCode">
              <a-input disabled v-model="model.barCode" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报价单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag type="list" v-model="model.unit" dictCode="product_unit" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number placeholder="请输入数量" v-model="model.num" class="w100"
                              @blur="quotedPriceTotalComputer"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input-number placeholder="请输入单价" v-model="model.unitPrice" class="w100" disabled></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="折扣" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discount">
              <a-input-number placeholder="请输入折扣" v-model="model.discount" class="w100"
                              @blur="discountComputer"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span=" 24
              ">
            <a-form-model-item label="售价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quotedPrice">
              <a-input-number placeholder="请输入售价" v-model="model.quotedPrice" class="w100"
                              @blur="quotedPriceComputer"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span=" 24
                ">
            <a-form-model-item label="报价小计" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quotedPriceTotal">
              <a-input-number v-model="model.quotedPriceTotal" disabled class="w100"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDepCustom'
import moment from 'moment'

export default {
  name: 'ProductInfoForm',
  components: {
    JSelectUserByDep
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        productId: '',//产品ID
        name: '',//产品名称
        number: '',//编号
        specifications: '',//规格
        barCode: '',//条码
        unit: '',//报价单位
        num: 1,//数量
        unitPrice: '',//单价
        discount: '',//折扣
        quotedPrice: '',//售价
        quotedPriceTotal: '',//报价小计
        remarks: ''//备注
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请选择产品!' }
        ],
        amountReceivable: [
          { required: true, message: '请输入回款金额!' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) > this.canBeAdd) {
                callback(new Error(`回款金额不能超过${this.canBeAdd}`))
              } else {
                callback()
              }
            },
            trigger: ['blur']
          }
        ],
        quotedPrice: [
          { required: true, message: '请输入售价!' }
        ],
        discount: [
          { required: true, message: '请输入折扣!' }
        ],
        unitPrice: [
          { required: true, message: '请输入单价!' }
        ],
        num: [
          { required: true, message: '请输入数量!' }
        ]
      },
      url: {
        getNumber: '/sys/common/getNumber',
        add: '/receivables/receivables/add',
        edit: '/receivables/receivables/edit',
        queryById: '/receivables/receivables/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    moment,
    add() {
      this.edit(this.modelDefault)

    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    //总价
    quotedPriceTotalComputer() {
      this.model.quotedPriceTotal = this.model.num * this.model.quotedPrice
    },
    //折扣率
    discountComputer() {
      this.model.quotedPrice = (this.model.unitPrice * this.model.discount / 100).toFixed(2)
      this.model.quotedPriceTotal = this.model.num * this.model.quotedPrice
    },
    //售价
    quotedPriceComputer() {
      this.model.discount = (this.model.quotedPrice / this.model.unitPrice * 100).toFixed(2)
      this.model.quotedPriceTotal = this.model.num * this.model.quotedPrice
    },
    onPopCellChange(key, dataIndex, value) {
      console.log('key', key)
      console.log('dataIndex', dataIndex)
      console.log('value', value)
      //产品ID
      this.model.productId = value.productId
      //名称
      this.model.name = value.name
      //编号
      this.model.number = value.number
      //规格
      this.model.specifications = value.specifications
      //产品条码
      this.model.barCode = value.barCode
      //报价单位
      this.model.unit = value.unit
      //数量
      // this.model.num = 1
      //单价
      this.model.unitPrice = value.unitPrice
      //折扣率
      this.model.discount = (value.unitPrice / value.unitPrice * 100).toFixed(2)
      //售价
      this.model.quotedPrice = value.unitPrice
      //报价小计
      this.model.quotedPriceTotal = this.model.totalProducts = value.unitPrice * this.model.num
      //备注
      // this.model.remarks = ''
    },
    submitForm() {
      const that = this
      that.confirmLoading = true
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = false
          that.$emit('ok', this.model)

        } else {
          that.confirmLoading = false
        }

      })
    }

  }
}
</script>
<style scoped lang="less">
.w100 {
  width: 100%;
}
</style>